package com.zbbt.sportslottery.ui.widget.imageshow;

import java.util.ArrayList;

import com.gc.materialdesign.views.ProgressBarCircularIndetermininate;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.util.ImageViewUtil;
import com.zbbt.sportslottery.util.LogUtil;
import com.zbbt.sportslottery.util.ScreenUtil;
import com.zbbt.sportslottery.util.ImageViewUtil.OnPushImageListener;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

/**
 * 图片浏览的PagerAdapter
 */
public class ImagePagerAdapter extends PagerAdapter {
	Context context;
	ArrayList<String> imgsUrl;
	LayoutInflater inflater = null;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	// view内控件
	TouchImageView[] full_image;
	ProgressBarCircularIndetermininate[] progress;
	TextView[] retry;
	Bitmap[] imgbitmap;

	public ImagePagerAdapter(Context context, ArrayList<String> imgsUrl, Bitmap[] imgbitmap) {
		this.context = context;
		this.imgsUrl = imgsUrl;
		inflater = LayoutInflater.from(context);
		full_image = new TouchImageView[imgsUrl.size()];
		progress = new ProgressBarCircularIndetermininate[imgsUrl.size()];
		retry = new TextView[imgsUrl.size()];
		this.imgbitmap = imgbitmap;
	}

	/** 动态加载数据 */
	@Override
	public void setPrimaryItem(ViewGroup container, int position, Object object) {
		super.setPrimaryItem(container, position, object);
		((ImageShowViewPager) container).mCurrentView = ((TouchImageView) ((View) object)
				.findViewById(R.id.full_image));
	}

	@Override
	public int getCount() {
		return imgsUrl == null ? 0 : imgsUrl.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public int getItemPosition(Object object) {
		// TODO Auto-generated method stub
		return super.getItemPosition(object);
	}

	@Override
	public Object instantiateItem(ViewGroup container, final int position) {
		View view = inflater.from(context).inflate(R.layout.item_details_imageshow, null);
		full_image[position] = (TouchImageView) view.findViewById(R.id.full_image);
		progress[position] = (ProgressBarCircularIndetermininate) view.findViewById(R.id.progress);
		retry[position] = (TextView) view.findViewById(R.id.retry);// 加载失败
		retry[position].setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				loadimg(position);
			}
		});
		loadimg(position);
		((ViewPager) container).addView(view);
		return view;
	}

	protected void loadimg(final int position) {
		// TODO Auto-generated method stub
		if (imgsUrl.get(position).substring(imgsUrl.get(position).lastIndexOf(".") + 1).equals("gif")) {
			ImageViewUtil.PushImageIntoImageView(imgsUrl.get(position), full_image[position],
					new OnPushImageListener() {
						@Override
						public void onPushImageEnd(ImageView imageview, Bitmap bitmap) {
							// TODO Auto-generated method stub
							imgbitmap[position] = bitmap;
							progress[position].setVisibility(View.GONE);
							full_image[position].setVisibility(View.VISIBLE);
							retry[position].setVisibility(View.GONE);
						}

						@Override
						public void onPushImageBegin() {
							// TODO Auto-generated method stub
							progress[position].setVisibility(View.VISIBLE);
							full_image[position].setVisibility(View.GONE);
						}
					});
		} else {
			imageLoader.displayImage(imgsUrl.get(position), full_image[position], App.option,
					new ImageLoadingListener() {
						@Override
						public void onLoadingStarted(String imageUri, View view) {
							// TODO Auto-generated method stub
							progress[position].setVisibility(View.VISIBLE);
							full_image[position].setVisibility(View.GONE);
							retry[position].setVisibility(View.GONE);
						}

						@Override
						public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
							// TODO Auto-generated method stub
							progress[position].setVisibility(View.GONE);
							full_image[position].setVisibility(View.GONE);
							retry[position].setVisibility(View.VISIBLE);
						}

						@Override
						public void onLoadingComplete(String imageUri, View view, Bitmap bitmap) {
							imgbitmap[position] = bitmap;
							progress[position].setVisibility(View.GONE);
							full_image[position].setVisibility(View.VISIBLE);
							retry[position].setVisibility(View.GONE);
						}

						@Override
						public void onLoadingCancelled(String imageUri, View view) {
							progress[position].setVisibility(View.GONE);
							full_image[position].setVisibility(View.GONE);
							retry[position].setVisibility(View.VISIBLE);
						}
					});
		}
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		((ViewPager) container).removeView((View) object);
	}
}
